package com.css.fxfzdpzs.util;

import ws.schild.jave.*;
import ws.schild.jave.encode.AudioAttributes;
import ws.schild.jave.encode.EncodingAttributes;
import ws.schild.jave.encode.VideoAttributes;

import java.io.File;
import java.io.IOException;

public class VideoConverter {
    /**
     * 视频转码
     * @param videoSource
     * @param videoTarget
     * @return true or false
     */
    public static boolean videoToVideo(String videoSource, String videoTarget) {
        long start = System.currentTimeMillis();

        File source = new File(videoSource);
        File target = new File(videoTarget);

        AudioAttributes audio = new AudioAttributes();
        audio.setCodec("aac");
        audio.setBitRate(236000 / 2);
        audio.setChannels(2);
        audio.setSamplingRate(8000);

        VideoAttributes video = new VideoAttributes();
        video.setCodec("h264");
        video.setBitRate(1000000);
        video.setFrameRate(25);
        video.setQuality(4);
//        video.setSize(new VideoSize(720, 480));

        EncodingAttributes attrs = new EncodingAttributes();
        attrs.setOutputFormat("mp4");
        attrs.setAudioAttributes(audio);
        attrs.setVideoAttributes(video);


        Encoder encoder = new Encoder();
        try {
            encoder.encode(new MultimediaObject(source), target, attrs);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(encoder.getUnhandledMessages());
            return false;
        }finally {
            long end = System.currentTimeMillis();
            System.out.println("总耗时："+ (end-start) +"ms");
        }
    }


    public static void main(String[] args) {
        String inputFilePath = "D:\\video/movie_007.wmv";
        String outputFilePath = "D:\\video/movie_007.mp4";
//        convertToMp4(inputFilePath, outputFilePath);
//        aviconvertmp4(new File(inputFilePath),new File(outputFilePath));
        videoToVideo(inputFilePath,outputFilePath);
    }
}